import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex)

import WebBluetooth from '../tqlsdk/WebBluetooth';
import * as DataName from '../utils/dataName';
import QwBluetooth from "../tqlsdk/QwBluetooth";


const state = {
    // isMobile: 'ontouchstart' in document.documentElement && window.orientation !== undefined && document.body.clientWidth < 900,
    isMobile: document.body.clientWidth < 900,
    event: require('../tqlsdk/event'),
    bluetooth: new WebBluetooth(),
    qwBluetooth : new QwBluetooth(),
    isConnect: false,
    //TqlCanvas 类
    tqlCanvas: null,
    lineWidth: 2,
    lineColor: '#000000',
    //是否开启笔锋
    isStroke: false,
    width: 1080,
    height: 1519.2,
    m_width: 750,
    m_height: 1055,
    backgroundMap: null,
    penDataList: [
        { label: DataName.BluetoothName, value: '', isChange: true },
        { label: DataName.BluetoothMAC, value: '', isChange: false },
        { label: DataName.PenType, value: '', isChange: false },
        { label: DataName.OfflineDataSize, value: '', isChange: false },
        { label: DataName.BluetoothFirmwareVersion, value: '', isChange: false },
        { label: DataName.MCUFirmwareVersion, value: '', isChange: false },
        { label: DataName.ElectricQuantity, value: '', isChange: false },
        { label: DataName.RealTimeClock, value: '', isChange: true },
        { label: DataName.AutoShutdownTime, value: '', isChange: true },
        { label: DataName.FlashProportion, value: '', isChange: false },
        { label: DataName.Buzzer, value: false, isChange: true },
        { label: DataName.PressureRouse, value: false, isChange: true },
        { label: DataName.PressureSensor, value: '', isChange: true },
        { label: DataName.PressureCorrection, value: '', isChange: false }
    ],
    defaultPenDataList: [
        { label: DataName.BluetoothName, value: '', isChange: true },
        { label: DataName.BluetoothMAC, value: '', isChange: false },
        { label: DataName.PenType, value: '', isChange: false },
        { label: DataName.OfflineDataSize, value: '', isChange: false },
        { label: DataName.BluetoothFirmwareVersion, value: '', isChange: false },
        { label: DataName.MCUFirmwareVersion, value: '', isChange: false },
        { label: DataName.ElectricQuantity, value: '', isChange: false },
        { label: DataName.RealTimeClock, value: '', isChange: true },
        { label: DataName.AutoShutdownTime, value: '', isChange: true },
        { label: DataName.FlashProportion, value: '', isChange: false },
        { label: DataName.Buzzer, value: false, isChange: true },
        { label: DataName.PressureRouse, value: false, isChange: true },
        { label: DataName.PressureSensor, value: '', isChange: true },
        { label: DataName.PressureCorrection, value: '', isChange: false }
    ],
    mobilePenDataList: [
        { label: DataName.BluetoothName, value: '', isLink: true },
        { label: DataName.BluetoothMAC, value: '', isLink: false },
        { label: DataName.BluetoothFirmwareVersion, value: '', isLink: false },
        { label: DataName.MCUFirmwareVersion, value: '', isLink: false },
        { label: DataName.CustomerId, value: '', isLink: true },
        { label: DataName.ElectricQuantity, value: '', isLink: false },
        { label: DataName.FlashProportion, value: '', isLink: false },
        { label: DataName.RealTimeClock, value: '', isLink: true },
        { label: DataName.Buzzer, value: false, isLink: false },
        { label: DataName.BuzzerChirp, value: '', isLink: true },
        { label: DataName.PressureRouse, value: false, isLink: false },
        { label: DataName.NotifyLight, value: false, isLink: false },
        { label: DataName.AutoShutdownTime, value: '', isLink: true },
        { label: DataName.PressureSensor, value: '', isLink: true },
        { label: DataName.PressureCorrection, value: '', isLink: false },
    ],
    isFirst: true,
}

const getters = {
    tqlCanvas: (state) => state.tqlCanvas,
    lineWidth: (state) => state.lineWidth,
    lineColor: (state) => state.lineColor,
    isStroke: (state) => state.isStroke,
    isFirst: (state) => { return state.isFirst },
    isMobile: (state) => { return state.isMobile },
    event: (state) => { return state.event },
    bluetooth: (state) => { return state.bluetooth },
    qwBluetooth: (state) => { return state.qwBluetooth },
    isConnect: (state) => { return state.isConnect },
    width: (state) => { return state.isMobile ? state.m_width : state.width },
    height: (state) => { return state.isMobile ? state.m_height : state.height },
    backgroundMap: (state) => { return state.backgroundMap },
    penDataList: (state) => { return state.penDataList },
    mobilePenDataList: (state) => { return state.mobilePenDataList }
}

const mutations = {
    setTqlCanvas: (state, tqlCanvas) => state.tqlCanvas = tqlCanvas,
    setLineWidth: (state, lineWidth) => {
        state.lineWidth = lineWidth
        state.tqlCanvas.lineWidth = lineWidth
    },
    setLineColor: (state, lineColor) => {
        state.lineColor = lineColor
        state.tqlCanvas.lineColor = lineColor
    },
    setIsStroke: (state, isStroke) => {
        state.isStroke = isStroke
        state.tqlCanvas.isStroke = isStroke
    },
    setIsFirst: (state, isFirst) => state.isFirst = isFirst,
    //修改背景图
    setBackgroundMap: (state, backgroundMap) => state.backgroundMap = backgroundMap,
    //修改连接状态
    setIsConnect: (state, isConnect) => state.isConnect = isConnect,
    //修改笔数据
    restorePenDataList: (state) => state.penDataList = state.defaultPenDataList,
}

const actions = {
    //监听-笔迹
    chirographyListener: ({ state }, funciton) => {
        state.event.on("AddressDataChanged", null, (data) => funciton(data))
    },
    //监听-笔迹
    penDataListener: ({ state }, funciton) => {
        state.event.on("penData", null, (data) => funciton(data))
    },
    //监听-连接
    connectListener: ({ state, commit }, funciton) => {
        state.event.on('connect', null, data => {
            //更改store中连接状态
            commit('setIsConnect', data)
            funciton(data)
        })
    },
    //监听-断开连接
    disconnectListener: ({ state }, funciton) => {
        state.event.on('disconnect', null, data => funciton(data))
    },
    //修改penDataList
    setPenDataList: ({ state }, data) => {
        state[state.isMobile ? 'mobilePenDataList' : 'penDataList'].find(i => i.label === data.label).value = data.value
    },
    //批量发送命令
    batchSendCommand: ({ state }) => {
        for (let i = 0; i < 11; i++) {
            setTimeout(() => {
                if (i === 0) state.bluetooth.getBluetoothName()
                if (i === 1) state.bluetooth.getBluetoothMAC()
                if (i === 2) state.bluetooth.getBluetoothFirmwareVersion()
                if (i === 3) state.bluetooth.getMCUFirmwareVersion()
                if (i === 4) state.bluetooth.getElectricQuantity()
                if (i === 5) state.bluetooth.getFlashProportion()
                if (i === 6) state.bluetooth.getRealTimeClock()
                if (i === 7) state.bluetooth.getAutoShutdownTime()
                if (i === 8) state.bluetooth.getBuzzerIsOpen()
                if (i === 9) state.bluetooth.getPressureSensor()
                if (i === 10) state.bluetooth.getPressureCorrection()
                // if (i === 11) state.bluetooth.getNotifyLight()
            }, 100 * i);
        }
    }
}

export default new Vuex.Store({
    state,
    getters,
    mutations,
    actions
})